package example.juc3;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class FutureTest {
    public static void main(String[] args) throws Exception {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("hello world");
                TimeUnit.SECONDS.sleep(5);
                return "hello callable";
            }
        });

        new Thread(futureTask).start();

        while (true) {
            if (futureTask.isDone()) {
                System.out.println(futureTask.get());// 如未执行完成，会阻塞，一般放在方法最后
                break;
            } else {
                TimeUnit.SECONDS.sleep(3);
                System.out.println("处理中...");
            }
        }


        // 过期不候，容易产生异常 System.out.println(futureTask.get(3, TimeUnit.SECONDS));

        System.out.println(Thread.currentThread().getName());
    }
}
